﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.Runtime.InteropServices;

namespace GOM_PLAYER
{
    public partial class user_defined_keyboard : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private Preferences preferences;
        process_xml process_xml = new process_xml();
        public user_defined_keyboard(Preferences pre2)
        {
            this.preferences = pre2;
            InitializeComponent();
        }
        public user_defined_keyboard()
        {
            InitializeComponent();
        }
        private void user_defined_keyboard_Load(object sender, EventArgs e)
        {
        }
       
        [DllImport("user32.dll")]
        private static extern short GetAsyncKeyState(Keys vKey);
        private bool KeyIsDown(Keys key)
        {
            return (GetAsyncKeyState(key) < 0);
        }

        private void shortcut_keys_comboBox_KeyDown(object sender, KeyEventArgs e)
        {        
            if(e.Control)
            {
                shortcut_keys_comboBox.MaxLength = 100;
                shortcut_keys_comboBox.Text="Ctrl + "+ e.KeyCode.ToString();
                if (shortcut_keys_comboBox.Text.IndexOf("ControlKey") > 0)
                    shortcut_keys_comboBox.Text = "Ctrl + ";
            }
            if (e.Alt)
            {
                shortcut_keys_comboBox.MaxLength = 100;
                shortcut_keys_comboBox.Text = "Alt + " + e.KeyCode.ToString();
                if (shortcut_keys_comboBox.Text.IndexOf("Menu") > 0)
                    shortcut_keys_comboBox.Text = "Alt + ";
            }
            if (e.Shift)
            {
                shortcut_keys_comboBox.MaxLength = 100;
                shortcut_keys_comboBox.Text = "Shift + " + e.KeyCode.ToString();
                if (shortcut_keys_comboBox.Text.IndexOf("ShiftKey") > 0)
                    shortcut_keys_comboBox.Text = "Shift + ";
            }
            if (e.Shift && e.Control)
            {
                shortcut_keys_comboBox.MaxLength = 100;
                shortcut_keys_comboBox.Text = "Shift + Ctrl + " + e.KeyCode.ToString();
                if (shortcut_keys_comboBox.Text.IndexOf("ShiftKey") > 0 || shortcut_keys_comboBox.Text.IndexOf("ControlKey") > 0)
                    shortcut_keys_comboBox.Text = "Shift + Ctrl + ";
            }
            if (e.Shift && e.Alt)
            {
                shortcut_keys_comboBox.MaxLength = 100;
                shortcut_keys_comboBox.Text = "Shift + Alt + " + e.KeyCode.ToString();
                if (shortcut_keys_comboBox.Text.IndexOf("ShiftKey") > 0 || shortcut_keys_comboBox.Text.IndexOf("Menu") > 0)
                    shortcut_keys_comboBox.Text = "Shift + Alt + ";
            }
            if (e.Control && e.Alt)
            {
                shortcut_keys_comboBox.MaxLength = 100;
                shortcut_keys_comboBox.Text = "Ctrl + Alt + " + e.KeyCode.ToString(); 
                if (shortcut_keys_comboBox.Text.IndexOf("CtrlKey") > 0 || shortcut_keys_comboBox.Text.IndexOf("Menu") > 0)
                    shortcut_keys_comboBox.Text = "Ctrl + Alt + ";
            }
            if (e.Control==false && e.Alt==false && e.Shift==false)
            {
                shortcut_keys_comboBox.Text = e.KeyCode.ToString();
                shortcut_keys_comboBox.MaxLength = 1;
            }        
        }

        private void ok_simpleButton_Click(object sender, EventArgs e)
        {
            shortcut_keyboard sk =new shortcut_keyboard();
            sk.shortcut_key = shortcut_keys_comboBox.Text.ToUpper();
            sk.description = command_combobox.Text;
            process_xml.insert_shortcut_keyboard(sk);
            preferences.main_window.list_shortcut_keyboard.Add(sk);  
            preferences.user_keyboard_defined_dataGridVew.DataSource = process_xml.load_shortcut_keyboard(); 
            this.Dispose();
        }
       
        private void cancel_simpleButton_Click(object sender, EventArgs e)
        {
            this.Dispose(); 
        }
       
    }
}